package io.renren.modules.hos.controller;



import com.zhenzi.sms.ZhenziSmsClient;
import io.renren.common.utils.CodeUtils;
import io.renren.common.utils.R;
import io.renren.modules.hos.entity.MedicineEntity;
import io.renren.modules.hos.service.CodeService;
import io.renren.modules.hos.service.MedicalAdviceService;
import io.renren.modules.hos.service.MedicineService;
import io.renren.modules.hos.service.impl.MedicalAdviceServiceImpl;
import io.renren.modules.hos.service.impl.MedicineServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("test")
public class TestController {
    @Autowired
    private CodeService codeService;

    /**
     * 获取验证码
     * @param phone
     * @return
     */
    @RequestMapping("code")
    public R getCode(String phone){
        Map<String, Object> map = codeService.sendMsg(phone);
        boolean flag = (boolean) map.get("flag");
        if (flag){
            return R.ok().put("data", map.get("uuid"));
        }
        else {
            return R.error("短信发送失败");
        }
    }

    @RequestMapping("/code1")
    public R send(String phone){
        String apiUrl = "https://sms_developer.zhenzikj.com";
        String appId = "110406";
        String appSecret = "1ff37082-7faf-44d5-871a-7ad381ceaee8";
        ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("number", phone);
        params.put("templateId", "7718");
        String[] templateParams = new String[3];
        templateParams[0] = "realName";
        templateParams[1] = "date";
        templateParams[2] = "time";

        params.put("templateParams", templateParams);
        String result = null;
        try {
            result = client.send(params);
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @Autowired
    private MedicineService medicineService;
    @RequestMapping("/med")
    public R add(){
        try {
            System.out.println(System.in);

            FileReader fileReader = new FileReader("C:\\Users\\lenovo\\Desktop\\1.txt");
            BufferedReader buf = new BufferedReader(fileReader);

            int i = 0;
            String bufToString = "";
            String readLine = "";
            String[] myArray = new String[600];  //100：这个值你自己定义，但不宜过大，要根据你文件的大小了，或者文件的行数
            while((readLine = buf.readLine()) != null){
                myArray[i] = readLine;
                i++;
            }
            Random random = new Random(2);
            for (int j = 0; j < 250; j++){
                MedicineEntity medicineEntity = new MedicineEntity();
                random.nextInt(300);
                medicineEntity.setMedicineName(myArray[j]);
                medicineEntity.setMedicineNumber(1000);
                medicineEntity.setCreateDatetime(new Date());
                medicineEntity.setMedicineManufacturer("贵阳药品公司");
                medicineEntity.setMedicineProductionDate(new Date());
                medicineEntity.setMedicineType(1);
                medicineEntity.setMedicineQualityDate(12);
                medicineEntity.setMedicinePrice(random.nextInt(50));

                medicineService.save(medicineEntity);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }
}
